const app = getApp();
 /**
   *调用腾讯地图显示位置信息
   * location :{
   *  latitude:纬度
   *  longitude：经度
   * }
   */
function reverseGeocoder(location) {
  let that = this
  return new Promise(function (resolve, reject) {
    app.globalData.qqmapsdk.reverseGeocoder({
      location: location,
      sig:'NjGMnQMAcZczYgicF6ubCPxOuLWhbqR',
      success: function (skdRes) {
        console.log(`请求用户的地址成功了吗？`,skdRes)
        // 坐标显示  
        let markers = []
        markers.push({
          iconPath: "/image/icon/biaoji.png",
          id: 0,
          latitude: location.latitude,
          longitude: location.longitude,
          width: 30,
          height: 30
        })
        //当前位置信息
        let region = []
        let province = skdRes.result.address_component.province
        let city = skdRes.result.address_component.city
        let district = skdRes.result.address_component.district
        let street_number = skdRes.result.address_component.street_number
        region.push(province)
        region.push(city)
        region.push(district)
        // 封装成一个对象返回
        let responseObj = {};
        responseObj.markers = markers;
        responseObj.region = region;
        responseObj.address = province + city + district;
        responseObj.addressDetail = street_number;
        // 如业务需求可继续补充数据
        resolve(responseObj);
      }
    })
  })
  
}

/**
  * 调用腾讯地图进行定位
  * 通过地址解析到经纬度
  * address:地址信息
  */
function geocoder(address) {
  let that = this
  return new Promise(function (resolve, reject) {
    // 调用腾讯地图进行定位
    app.globalData.qqmapsdk.geocoder({
      address: address,
      sig:'NjGMnQMAcZczYgicF6ubCPxOuLWhbqR',
      success: function (response) {
        console.log(`解析成功啦！`, response)
        resolve(response)
      }
    })
  }) 
}

module.exports = {
  reverseGeocoder: reverseGeocoder,
  geocoder: geocoder
}